package org.example.rocketmq;


import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Auther: wangyaomeng
 * @Date: 2021/1/18 13:43
 * @param:
 */
@Component
public class StudyProducer
{
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    private static final String STUDY_PRODUCER_TOPIC="topic-study";


    public void syncSend(){
        User user = new User("老王", "男", "同步发送消息");
        SendResult sendResult = rocketMQTemplate.syncSend(STUDY_PRODUCER_TOPIC + ":tag1", user);
        System.out.println("同步发送消息:"+sendResult);
    }

    public void asyncSend(){
        User user = new User("老王", "男", "异步发送信息");
        rocketMQTemplate.asyncSend(STUDY_PRODUCER_TOPIC + ":tag2", user, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("异步消息发送成功:"+sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("异步消息发送异常:"+throwable);
            }
        });
    }

    public void sendOneway(){
        User user = new User("老王", "男", "单向发送消息");
        rocketMQTemplate.sendOneWay(STUDY_PRODUCER_TOPIC + ":tag3", user);
        System.out.println("单向发送消息成功");
    }



}
